﻿using ERPPublic;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using WarehouseManagement.Application.Command.ConfirmTheOutboundCommand;
using WarehouseManagement.Application.Command.OutboundShipmentsCommand;
using WarehouseManagement.Domain.DTO.OutvoundRequests;

namespace WarehouseManagement.Controllers
{
    /// <summary>
    /// 出库控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [Authorize]
    [ApiController]
    public class OutboundShipmentsController : ControllerBase
    {
        protected readonly IMediator mediator;

        public OutboundShipmentsController(IMediator mediator)
        {
            this.mediator = mediator;
        }
        /// <summary>
        /// 出库申请新增
        /// </summary>
        /// <param name="cd"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> ProcureEnquiryAdd(OutboundShipmentsCreateCommand cd)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
                cd.dtos.Office_Id = sid;
                var name = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//获取用户名
                cd.dtos.AddName = name;
                //获取用户ID
                var userId = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
                cd.dtos.User_Id = int.Parse(userId);
                result.Data = await mediator.Send(cd);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Code = -1;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 出库申请显示
        /// </summary>
        /// <param name="cd"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<OutboundRequestSelectDto>> ProcureEnquiryShow([FromQuery] OutboundShipmentsSelectCommand cd)
        {
            ApiResult<OutboundRequestSelectDto> result = new ApiResult<OutboundRequestSelectDto>();
            try
            {
                result.Data = await mediator.Send(cd);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Code = -1;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 出库申请删除
        /// </summary>
        /// <param name="cd"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> ProcureEnquiryDelete(OutboundShipmentsDeleteCommand cd)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                var name = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//获取用户名
                cd.DelName = name;
                result.Data = await mediator.Send(cd);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Code = -1;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 出库申请反填
        /// </summary>
        /// <param name="cd"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<OutboundRequestSetDto>> ProcureEnquirySetInfo([FromQuery] OutboundShipmentsSetCommand cd)
        {
            ApiResult<OutboundRequestSetDto> result = new ApiResult<OutboundRequestSetDto>();
            try
            {
                result.Data = await mediator.Send(cd);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Code = -1;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 出库申请修改
        /// </summary>
        /// <param name="cd"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> ProcureEnquiryUpdate(OutboundShipmentsUpdateCommand cd)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
                cd.dtos.Office_Id = sid;
                var name = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//获取用户名
                cd.dtos.UpdName = name;
                //获取用户ID
                var userId = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
                cd.dtos.User_Id = int.Parse(userId);
                result.Data = await mediator.Send(cd);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Code = -1;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 确认出库
        /// </summary>
        /// <param name="cd"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> ProcureEnquiryUpdateOk(ConfirmTheOutboundCreateCommand cd)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {             
                result.Data = await mediator.Send(cd);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Code = -1;
                throw;
            }
            return result;
        }
    }
}
